﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Composite.Regions;

#region namespaces model-view

using EQual.MainModule.Employees.Views;
using EQual.MainModule.Employees.Models;

#endregion

using EQual.Infrastructure;


namespace EQual.MainModule.Employees.Presenters
{
    public class EmployeesController : EQual.Infrastructure.MVPFramework.Presenters.ControllerBase<IEmployeeGridView , DataAccesLayer .Employee >, EQual.MainModule.Employees.Presenters.IEmployeesController, EQual.Infrastructure.IModuleController 
    {

        DataAccesLayer.EmployeeDataAccesLayer accesLayer;
        EQual.MainModule.DetailsContainer containerView = new DetailsContainer();

        public DataAccesLayer.EmployeeDataAccesLayer AccesLayer
        {
            get { return accesLayer; }
            set { accesLayer = value; }
        }

        IRegionManager _regionManager;

        IEmployeesPresentationModel ViewModel;

        IEmployeeGridView _view_Main;

        public IEmployeeGridView View
        {
            get { return _view_Main; }
            set { _view_Main = value; }
        }

        public EmployeesController(IRegionManager regionManager)
            : this()
        {
            this._regionManager = regionManager;

        }

        public EmployeesController()
        {
            accesLayer = new DataAccesLayer.EQualDataLayerFactory().GetEmployeeAccesLayer();
            InitializeViews();

        }

       //secondary Presenters and Views 
       DetailsViews.EmployeeDetailsPresenter detailsPresenter;
       DetailsViews.EmployeeDistributionPresenter distributionPresenter;


       public override void Dispaly(DataAccesLayer.Employee item)
       {

           base.Dispaly(item);
       }

       public void Run()
       {
           ViewModel = new EQual.MainModule.Employees.Models.EmployeesPresentationModel();
           View = new EQual.MainModule.Employees.Views.EmployeeGridView();

           ViewModel.View = View;
           View.Model = ViewModel;
         
           View.ItemSelected += new EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object >>(View_ItemSelected);
           View.ItemDeleted += new EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object >>(View_ItemDeleted);
           View.ItemAdded += new EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object >>(View_ItemAdded);

           detailsPresenter = new EQual.MainModule.Employees.DetailsViews.EmployeeDetailsPresenter();
           distributionPresenter = new EQual.MainModule.Employees.DetailsViews.EmployeeDistributionPresenter();

           this.AddPresenter(detailsPresenter);
           this.AddPresenter(distributionPresenter);

           this._regionManager.RegisterViewWithRegion(RegionNames.MainRegion,() => this.View);
           this._regionManager.Regions[RegionNames.MainRegion].Activate(this.View);
       }

       void View_ItemAdded(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<object> e)
       {
           RequestNewItemCreation(e.Value as DataAccesLayer .Employee );
       }

       void View_ItemSelected(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<object> e)
       {
           InjectViews();
           if (e.Value == null) return;

           distributionPresenter.Dispaly(e.Value as DataAccesLayer .Employee );
           dff.DispalyDocumentDetails(e.Value as DataAccesLayer.Employee);
           projectsOwner.Model = new {
               ItemsCollection = new DataAccesLayer.EQualDataLayerFactory().GetProjectsAccesLayer().GetProjectOwnToEmployee(e.Value as DataAccesLayer.Employee)
           };
       }

       void View_ItemDeleted(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<object > e)
       {
           RequestItemDelete(e.Value as DataAccesLayer.Employee);
       }

       DetailsViews.EmployeeDetailedControl   dff = new DetailsViews.EmployeeDetailedControl ();
       Projects.Views.ProjectsGridView projectsOwner = new EQual.MainModule.Projects.Views.ProjectsGridView();
       
       protected virtual void InjectViews()
       {
           //throw in to the datails region the datails view
           IRegion region = _regionManager.Regions[RegionNames.ActionRegion];

           if (!region.Views.Contains(containerView))
           {
               containerView.AddView(GetDecoratedView(detailsPresenter.View,"Λεπτομέρειες"));
               containerView.AddView(GetDecoratedView(distributionPresenter.View,"Δίανομή"));
               containerView.AddView(GetDecoratedView(dff, "Λεπτομέρειες"));
               containerView.AddView(GetDecoratedView(projectsOwner, "Project Owner"));
               region.Add(containerView);
           }

           //activate the current view
           region.Activate(containerView);
       }

       public object GetDecoratedView(object view,string header)
       {
           return new DataGridDecorator(view, header);
       }

       
       public void SubmitChanges()
       {
          
       }

       public void RequestNewItemCreation(DataAccesLayer.Employee  empl)
       {
           AccesLayer.AddNewEmployee(empl);
       }

       public void RequestItemDelete(DataAccesLayer.Employee empl)
       {
           AccesLayer.DeleteEmployee(empl);
       }

       internal void RequestItemDelete(System.Collections.IList iList)
       {
           AccesLayer.DeleteEmployee(iList);
       }

    }
}
